package pub.cleangao.mqtt.processor;

import org.json.JSONObject;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Base64;

@Component
public class IvsResultProcessor extends AbstractProcessor {


    @Override
    public String topic() {
        return "device/+/message/up/ivs_result";
    }

    @Override
    public void process(String msgContent) {
        JSONObject jsonObject = new JSONObject(msgContent);
        JSONObject plate = jsonObject
                .getJSONObject("payload")
                .getJSONObject("AlarmInfoPlate")
                .getJSONObject("result")
                .getJSONObject("PlateResult")
                .getJSONArray("plates")
                .getJSONObject(0);
        String license = null;
        try {
            String licenseBase64 = plate.getString("license");
            license = new String(Base64.getDecoder().decode(licenseBase64.getBytes(Charset.forName("utf-8"))), "GBK");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        int color = plate.getInt("color");
        int type = plate.getInt("type");
        System.out.println(super.serialNo + ", type:" + type + ",color:" + color + ",license:" + license);
    }

}
